/**
 * Copyright (C), 2019-2020,个人开发
 * 文件名: SsoSessionIdHelper1
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/2/24 15:09
 */
package com.zx.sso.core.store;

import com.zx.sso.core.user.ZxSsoUser;

/**
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/2/24 15:09
 * 描述:
 * make client sessionId
 * <p>
 * client: cookie = [userid#version]
 * server: redis
 * key = [userid]
 * value = user (user.version, valid this)
 * <p>
 * group         The same group shares the login status, Different groups will not interact
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public class SsoSessionIdHelper {

    /**
     * make client sessionId
     *
     * @param xxlSsoUser
     * @return
     */
    public static String makeSessionId(ZxSsoUser xxlSsoUser) {
        String sessionId = xxlSsoUser.getUserid().concat("_").concat(xxlSsoUser.getVersion());
        return sessionId;
    }

    /**
     * parse storeKey from sessionId
     *
     * @param sessionId
     * @return
     */
    public static String parseStoreKey(String sessionId) {
        if (sessionId != null && sessionId.indexOf("_") > -1) {
            String[] sessionIdArr = sessionId.split("_");
            if (sessionIdArr.length == 2
                    && sessionIdArr[0] != null
                    && sessionIdArr[0].trim().length() > 0) {
                String userId = sessionIdArr[0].trim();
                return userId;
            }
        }
        return null;
    }

    /**
     * parse version from sessionId
     *
     * @param sessionId
     * @return
     */
    public static String parseVersion(String sessionId) {
        if (sessionId != null && sessionId.indexOf("_") > -1) {
            String[] sessionIdArr = sessionId.split("_");
            if (sessionIdArr.length == 2
                    && sessionIdArr[1] != null
                    && sessionIdArr[1].trim().length() > 0) {
                String version = sessionIdArr[1].trim();
                return version;
            }
        }
        return null;
    }
}
